Pokerface.addComponent(new Pokerface.Component.extend({
    // protected methods
    _validate: function(str) {
        return /^\w{3,20}$/.test($.trim(str));
    },

    // public
    getName: function(){
       return 'login';
    },

    init: function(){
       ;
    },
    
    start: function() {
        var self = this;
        $('#login_actions_switch a').click(function(){
            $('#login, #register').toggle(200);
            return false;
        });
        $('#register form').submit(function(){
            if (!self._validate($('#reg_login').val())) {
                return self._showError('WRONG_LOGIN_SYMBOLS');
            }
            if (!self._validate($('#reg_pass').val())) {
                return self._showError('WRONG_PASS_SYMBOLS');
            }
            if ($('#reg_pass').val() != $('#reg_confirm_pass').val()) {
                return self._showError('PASS_DO_NOT_MATCH');
            }
            $(this).ajaxSubmit({
                dataType: 'json',
                success: function(result) {
                    if (!result.state) {
                        self._showError('LOGIN_EXISTS');
                    } else {
                        // reload for looking at a new view
                        window.location = '/game/game';
                    }
                }
            });
            return false;
        });
        $('#login form').submit(function(){
            $(this).ajaxSubmit({
                dataType: 'json',
                success: function(result) {
                    if (!result.state) {
                        self._showError('WRONG_CREDENTIALS');
                    } else {
                        // reload for looking at a new view
                        window.location = '/game/game';
                    }
                }
            });
            return false;
        });
    }
})());